export async function loadResource(resPath: string,  progressCallback: (completeCount: number, totalCount: number, item: any) => void)
{
    for(let i = 0; i < maxRetryNum; i++)
    {
        try
        {
            let asset:cc.Asset = await loadResourceInternal(resPath, progressCallback);
            return asset;
        }
        catch(e)
        {
            await waitInterval(waitRetryInterval);
        }
    }

    return null;
}

function loadResourceInternal(resPath: string, progressCallback: (completeCount: number, totalCount: number, item: any) => void): Promise<any>  
{
    return new Promise<cc.Asset>((resolve, reject) =>{
        cc.loader.loadRes(resPath,
            progressCallback,
            function(err, prefab){
            if(err)
            {
                console.log(err["message"]);
                reject(err);
            }
            else
            {
                resolve(prefab);
            }
        })
    });
}

export async function loadWWW(url: string, progressCallback: (completeCount: number, totalCount: number, item: any) => void): Promise<cc.Texture2D>  
{
    return new Promise<cc.Texture2D>((resolve, reject) =>{
        cc.loader.load(url,
            progressCallback,
            function(err, texture){
            if(err)
            {
                console.log(err["message"]);
                reject(err);
            }
            else
            {
                resolve(texture);
            }
        })
    });
}

export async function waitInterval(ms: number): Promise<any>
{
    return new Promise<cc.Asset>((resolve, reject) =>{
        setTimeout(function()
        {
            resolve();
        }, ms);
    });
}

const maxRetryNum: number = 300;
const waitRetryInterval: number = 1000;